<?php
/**
 * @copyright: ©2024 永州 微聚 商 网络科技有限公司
 * @link: https://www.weiJuyunke.com
 */
namespace app\api\controller\consume;
use app\common\controller\AdminController;
use think\App;

/**
 * @ControllerAnnotation(title="会员管理")
 */
class ClaimLog extends AdminController
{
    
    use \app\admin\traits\Curd;

    /**
     * 字段排序
     *
     * @var array
     */
    protected $sort = [
        'create_time' => 'desc'
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);
        
        $this->model = new \app\admin\model\ConsumerClaimLog();
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {

        $request = $this->request;
        $param = $request->param();
        $page = input("page");
        $limit = input("limit");
        $where[] = ['type','=',1,'is_delete' => 0];

        if ($this->request->platform_id >0 ){
            $where[] = ['platform_id','=',$this->request->platform_id];
        }
        if (isset( $param['telphone']) &&   $param['telphone']){
            $where[] = ['userInfo.telphone','like',"%{$param['telphone']}%"];
        }

        if (isset( $param['name']) &&   $param['name']){
            $where[] = ['name','like',"%{$param['name']}%"];
        }

        if (isset( $param['platform_name']) &&   $param['platform_name'] ){
            $where[] = ['platformInfo.name','=',$param['platform_name']];
        }
        $count = $this->model->withJoin([
            'platformInfo',
            'userInfo',
            'storeInfo',
            'consumerInfo'
        ], 'LEFT')
            ->where($where)
            ->count();
        $list = $this->model->withJoin([
            'platformInfo',
            'userInfo',
            'storeInfo',
            'consumerInfo'
        ], 'LEFT')
            ->where($where)
            ->page($page, $limit)
            ->order($this->sort)
            ->select();
        
        return msg([
            'list' => $list,
            'count' => $count
        ], 0, "success");
    }

    public function add(){
        $request = $this->request;
        $param = $request->param();
        $StoreConsumerModel  =  new \app\admin\model\Consumer();
        $StoreConsumerData = [
            'deductible_amount' => $param['deductible_amount'],
            'satisfy_amount' => $param['satisfy_amount'],
            'consumer_type' =>  $param['consumer_type'],
            'expire_time' => strtotime($param['expire_time']),
            'type' => 1,
            'platform_id'=> $this->request->platform_id,
        ];
        if (isset($param['id']) && $param['id']){
            $StoreConsumerModel->where(['id' => $param['id']])->update($StoreConsumerData);
        }else{
            $StoreConsumerModel->save($StoreConsumerData);
        }

        return msg([], 0, "添加成功!");
    }

    public function edit(){
        $request = $this->request;
        $param = $request->param();
        $StoreConsumerModel  =  new \app\admin\model\Consumer();
        $StoreConsumerData = [
            'deductible_amount' => $param['deductible_amount'],
            'satisfy_amount' => $param['satisfy_amount'],
            'consumer_type' =>  $param['consumer_type'],
            'expire_time' => strtotime($param['expire_time']),
            'type' => 1,
            'platform_id'=> $this->request->platform_id,
        ];
        if (isset($param['id']) && $param['id']){
            $StoreConsumerModel->where(['id' => $param['id']])->update($StoreConsumerData);
        }else{
            $StoreConsumerModel->save($StoreConsumerData);
        }

        return msg([], 0, "修改成功!");
    }


    public function delete(){
        $request = $this->request;
        $param = $request->param();
        $StoreConsumerModel  =  new \app\admin\model\Consumer();
        $StoreConsumerModel->where(['id' => $param['id']])->update(['is_delete' =>1]);
        return msg([], 0, "删除成功!");
    }


    public function consumerList(){
        $request = $this->request;
        $param = $request->param();
        $page = input("page");
        $limit = input("limit");
        $where[] = ['type','=',2,'is_delete' => 0];

        if ($this->request->platform_id >0 ){
            $where[] = ['platform_id','=',$this->request->platform_id];
        }
        if (isset( $param['name']) &&   $param['name']){
            $where[] = ['name','like',"%{$param['name']}%"];
        }
        if (isset( $param['platform_name']) &&   $param['platform_name'] ){
            $where[] = ['platformInfo.name','=',$param['platform_name']];
        }
        if (isset( $param['store_name']) &&   $param['store_name'] ){
            $where[] = ['storeInfo.store_name','=',$param['store_name']];
        }
        $count = $this->model->withJoin([
            'platformInfo',
            'storeInfo'
        ], 'LEFT')
            ->where($where)
            ->count();
        $list = $this->model->withJoin([
            'storeInfo',
            'platformInfo'
        ], 'LEFT')
            ->where($where)
            ->page($page, $limit)
            ->order($this->sort)
            ->select();

        return msg([
            'list' => $list,
            'count' => $count
        ], 0, "success");
    }
}